$(function (event) {


    var totalValue = 0;
    calTotalMoney();


    //购物车选择后更新价格
    $("input[name=selectProduct]").change(function (event) {

        calTotalMoney();
    });


    //提交订单,此时提交都是未支付的状态，也先不考虑15分钟有效期什么的
    $("#submitOrder").on('click', function (event) {
        //先获取到购物车里面所有的商品
        var productList = [];
        $("input[name=selectProduct]:checked").each(function () {
            var product = {
                barCode: $(this).attr('data'),
                salePrice: $(this).val()
            };
            productList.push(product);
        });

        $.ajax({
            url: '/order/gotoOrder',
            type: 'post',
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify(productList),
            success: function (datas) {

                if(datas.error==="100"){
                    alert(datas.message);
                    window.location.href='/cart/showCart';
                }else{
                    var orderId = datas['object']['orderId'];
                    window.location.href = '/order/showPay?orderId=' + orderId;
                }




            }

        })

    });

    //从购物车中删除物品
    $("input[name=deleteCart]").on("click", function (event) {
        var cartId = $(this).attr('data');
        $.ajax({
            url: '/cart/deleteCart/' + cartId,
            success: function (datas) {
                window.location.reload();
            }
        })
    });

    function calTotalMoney() {

        totalValue = 0;

        //获取所有为已选中的checkBox
        $("input[name=selectProduct]:checked").each(function () {
            totalValue += parseFloat($(this).val());
        });
        //更新结算区的价格
        $("#totalMoney").text("￥ " + totalValue);
    }

});